package customview.demo;

import android.app.Activity;
import android.content.Context;
import android.graphics.Rect;
import android.os.Bundle;
import android.widget.Button;
import android.widget.LinearLayout;

public class CustomViewActivity extends Activity {
    /** Called when the activity is first created. */
	/*
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        
        LinearLayout mainLayout= createLayout(LinearLayout.VERTICAL);
        mainLayout.setBackgroundResource(R.drawable.bg);
        
        ButtonView button1 = new ButtonView(this, 100, 100);
        ButtonView button2 = new ButtonView(this, 100, 100);
        ButtonView button3 = new ButtonView(this, 100, 100);


        LinearLayout contentLayout = createLayout(LinearLayout.VERTICAL);
        contentLayout.addView(button1);
        contentLayout.addView(button2);
        contentLayout.addView(button3);
        
        mainLayout.addView(contentLayout);
        setContentView(mainLayout);
    }
    
    public LinearLayout createLayout(int orientation) {
    	LinearLayout layout = new LinearLayout(this);
    	// 设置布局参数
    	layout.setLayoutParams(new LayoutParams(
    			android.view.ViewGroup.LayoutParams.FILL_PARENT,
    			android.view.ViewGroup.LayoutParams.WRAP_CONTENT));
    	
    	layout.setOrientation(orientation);
    	
    	return layout;
    }
    */
	
	private ExButton ebFirst,ebSecond,ebThird;
	/** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        //调用父类的onCreate方法
    	super.onCreate(savedInstanceState);
    	//初始化视图，并加载布局
        this.initiazlieViews();
    }
 
    private void initiazlieViews(){
    	//实例化ExButton对象，并设置其显示值
    	ebFirst=new ExButton(this);
    	ebFirst.setText("the first");
    	ebSecond=new ExButton(this);
    	ebSecond.setText("the second");
    	ebThird=new ExButton(this);
    	ebThird.setText("the third");
    	//实例化一个LinearLayout来存放这些ExButton
    	LinearLayout llContainer=new LinearLayout(this);
    	//设置控件的排列方向，这一点很重要，不要设置成水平方向
    	llContainer.setOrientation(LinearLayout.VERTICAL);
    	//将ExButton对象加入llContainer中
    	llContainer.addView(ebFirst);
    	llContainer.addView(ebSecond);
    	llContainer.addView(ebThird);
    	//设置contentView
    	  setContentView(llContainer);
    }
    
    
    class ExButton extends Button{
    	/**
    	 * 必须实现的构造方法
    	 * @param context  上下文对象
    	 */
		public ExButton(Context context) {
			super(context);
		}
 
		@Override
		protected void onFocusChanged(boolean focused, int direction,
				Rect previouslyFocusedRect) {
			//处理事件
			System.out.println("the button "+this.getText()+"  focused is "+focused+" and the direction is "+direction
				);
			//第一次获取焦点之前previouslyFocusedRect为nul
			if(previouslyFocusedRect!=null){
				System.out.println(	"the previouslyFocusedRect.centerx is  "+previouslyFocusedRect.centerX()
						+"the previouslyFocusedRect centery is  "+previouslyFocusedRect.centerY());
			}
			super.onFocusChanged(focused, direction, previouslyFocusedRect);
		}
 
 
    }
}